package uk.ac.bbk.dcs.muc;

/**
 * Copyright [2011] [Sean Howes and Aruna Gunasekera]
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 */
import static uk.ac.bbk.dcs.muc.FileWritter.WriteToFile;
import static uk.ac.bbk.dcs.muc.KmlBuilder.buildKmlFromDatabase;
import android.app.Activity;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.SeekBar;
import android.widget.TextView;

public class LifeLog extends Activity {

	private String FILENAME = "lifelog";
	private String EXT = ".kml";
	private String DIR_NAME = "LifeLog";
	private TextView seekValue;
	private PendingIntent pendingIntent;
	

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		
		final Intent mobileService = new Intent(this, MobileService.class);
		
		final Button btnStart = (Button) findViewById(R.id.button_start);
		final Button btnStop = (Button) findViewById(R.id.button_stop);
		btnStop.setEnabled(false);
		btnStart.setEnabled(false);

		/**
		 * slider for the frequency
		 */
		final SeekBar seekBar = (SeekBar)findViewById(R.id.seekbar);
		seekValue = (TextView)findViewById(R.id.seekvalue);
        seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener(){
        	Bundle bundle = new Bundle();	
        	@Override
   	      public void onProgressChanged(SeekBar arg0, int arg1, boolean arg2) {
   	       seekValue.setText(String.valueOf(arg1) + " Seconds");
   	       bundle.putInt("timeFreq", Integer.parseInt(String.valueOf(arg1)));
   	       mobileService.putExtras(bundle);
   	       btnStart.setEnabled(true);
   	      }
   	     
   	      @Override
   	      public void onStartTrackingTouch(SeekBar arg0) {}
   	     
   	      @Override
   	      public void onStopTrackingTouch(SeekBar arg0) {}    	
        	
        });
			
        pendingIntent = PendingIntent.getService(this, 0, mobileService, 0);
		
		
		btnStart.setOnClickListener(new View.OnClickListener() {
			public void onClick(View view) {
					startService(mobileService);
					seekBar.setEnabled(false);
					btnStop.setEnabled(true);
					btnStart.setEnabled(false);
			}
		});
	
		
		final EventSqlLiteHelper helper = new EventSqlLiteHelper(this);

		btnStop.setOnClickListener(new View.OnClickListener() {
			public void onClick(View view) {
					btnStart.setEnabled(true);
					stopService(mobileService);
					String data = buildKmlFromDatabase(helper.getReadableDatabase());
					WriteToFile(data, DIR_NAME, FILENAME+System.currentTimeMillis()+ EXT);
					btnStop.setEnabled(false);
					seekBar.setEnabled(true);
			}
		});
	}
}